/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 UJF.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.sensor.proxy;

import org.json.JSONException;
import org.json.JSONObject;
import org.osami.gateway.sensor.data.SenseData;
import org.osgi.util.measurement.Measurement;
import org.osgi.util.measurement.Unit;

public final class SenseDataJson implements SenseData {

    protected static final String UNIT_KEY = "unit";

    protected static final String VALUE_KEY = "InstatenousRate";

    private static final String TYPE_KEY = "name";

    private JSONObject jobject;

    private final Measurement measurement;

    private final Long timestamp;

    private final String senseid;

    private final String type;
    
    private final String location;

    public SenseDataJson(JSONObject json, String pSenseid) {
        jobject = json;
        try {
            String unit = json.getString(UNIT_KEY);
            Double value = json.getDouble(VALUE_KEY);
            type = json.getString(TYPE_KEY);
            location = json.getString("location");
            measurement = new Measurement(value, stringToUnit(unit));
            timestamp = System.currentTimeMillis();
            senseid = pSenseid;
            
        } catch (JSONException e) {
            throw new IllegalArgumentException("Illegal SenseData", e);
        }

    }

    public String getUnit() {
        try {
            return jobject.getString(UNIT_KEY);
        } catch (JSONException e) {
            // TODO
            return null;
        }
    }

    public Double getValue() {
        return measurement.getValue();
    }

    public String toJson() {
        return jobject.toString();
    }

    public Measurement toMeasurement() {
        return measurement;
    }

    // Convert String to Unit
    private final static Unit stringToUnit(String stringUnit) {

        if (stringUnit.matches("[Ll][uU][xX]|[Ll][xX]")) {
            return Unit.lx;
        }

        else {
            return Unit.unity;
        }
    }

    public String getSenseId() {
        return senseid;
    }

    public Long getTimeStamp() {
        return timestamp;
    }

    public String getType() {
        return type;
    }

    public String getLocation() {
        return location;
    }

}
